package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入购物车的商品是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果存在就进行数量加一
        // 检查购物车列表是否为空，如果不为空，则获取列表中的第一个购物车对象
        if (list != null && list.size() > 0) {
            ShoppingCart cart = list.get(0);
            // 对第一个购物车的数量进行加一操作，模拟添加商品到购物车
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        } else {
            //如果不存在就添加到购物车
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //本次添加的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //本次添加的是套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }


    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart userId = ShoppingCart.builder()
                .userId(currentId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(userId);
        return list;
    }

    public void cleanShoppingCart() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(currentId);
    }

    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 创建一个购物车实例，用于后续操作
        ShoppingCart shoppingCart = new ShoppingCart();
        // 将购物车DTO的信息复制到购物车实例中
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        // 获取当前登录用户的ID，用于设置购物车的用户ID
        // 设置查询条件，查询当前登录用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        // 设置购物车的用户ID，以便查询特定用户的购物车信息
        shoppingCart.setUserId(currentId);

        // 根据当前用户的购物车信息进行查询
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        // 如果查询结果存在且不为空
        if (list != null && list.size() > 0) {
            // 用查询结果中的第一个购物车实例来更新当前购物车实例
            shoppingCart = list.get(0);
            // 获取购物车的数量
            Integer number = shoppingCart.getNumber();
            // 如果数量为1，表示购物车中只有一个商品，直接删除购物车记录
            if (number == 1) {
                shoppingCartMapper.deleteById(shoppingCart.getId());
            } else {
                // 如果数量大于1，减少商品数量，并更新数据库
                shoppingCart.setNumber(number - 1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }

    }
}
